home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: Pointer Conversion
- Date: 21 Jan 1996 12:42:20 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4dttts$b70@umbc9.umbc.edu>
- References: <4ds4jq$fo4@su3.in.net>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Sam Pounds <poundss@in.net> wrote:
- |> I am having a problem with a "string concatenate" function.
- |> When I compile my little program it works, but I get a
- |> "suspicious pointer" conversion warning. The function is
- |> below and I call it with two strings that I want to concatenate.
- |>
- |> char *my_strcat(const char *a, const char *b)
- |> {
- |> char done[1024];
- |> char *p = done;
- |>
- |> while (*a)
- |> *p++ = *a++;
- |> while (*b)
- |> *p++ = *b++;
- |> *p = '\0';
- |> return done; /* this is the suspicious pointer conversion error */
- |> }
- |>
- |> I'm sure someone can explain this to me.
- |> Thanks in advance.
-
- First of all your function has different semantics from strcat() which
- may or may not be what you intended. In the <string.h> version only 'b'
- is const and 'a' is actually the string things are added onto. Now
- Everything looks pretty good up to the point where you return 'done'.
- No matter how similar in semantics pointers and arrays can be used
- they are not the same type. Pointers can be returned and arrays cannot.
- This is because returning an array is really returning a pointer to
- the first element, but the array is a local variable so this space
- goes away after the function ends.
-
- What you need is to either make 'done' a static variable and have the
- calling function handle memory allocation, or else allocate the space
- inside of my_strcat() and return a legally allocated block of memory.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-